package com.usian.service.impl;

import com.usian.pojo.TbItem;
import com.usian.redis.RedisClient;
import com.usian.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisClient redisClient;

    @Value("${CART_REDIS_KEY}")
    private String CART_REDIS_KEY;

    @Override
    public boolean insertItemToRedis(String userId, Map<String, TbItem> cart) {
        boolean bool = redisClient.hset(CART_REDIS_KEY, userId, cart);
        return bool;
    }

    @Override
    public Map<String, TbItem> selectCartByUserId(String userId) {
//        从redis中取购物车信息  hash
        Map<String, TbItem> map = (Map<String, TbItem>) redisClient.hget(CART_REDIS_KEY, userId);
        return map;
    }
}
